﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using HNCDataSetTableAdapters;

public partial class Admin_Project_ProjectDetail : System.Web.UI.Page
{
    CategoryTableAdapter cate = new CategoryTableAdapter();
    ProductsTableAdapter pro = new ProductsTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //LoadCateProduct(4);
            if (Request.QueryString["ProductID"] != null)
            {
                LoadProductDetail(int.Parse(Request.QueryString["ProductID"].ToString()));
            }

        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool vip = false;
        if (cbVip.Checked)
        {
            vip = true;
        }
        bool status = false;
        if (cbStatus.Checked)
        {
            status = true;
        }

        if (Request.QueryString["ProductID"] == null)
        {
            string image = "";
            if (FileUpload1.FileName.Trim() != "" && FileUpload1.FileName != null)
            {
                string[] arr = FileUpload1.FileName.Split('.');
                string ext = arr[arr.Length - 1];

                for (int j = 0; j < arr.Length - 1; j++)
                {
                    image = image + arr[j];
                }

                image = image + FuntionHNC.ThemChuoiDatetime();
                image = image + "." + arr[arr.Length - 1];

                image = image.Replace('%', '-');

                String filePath = "~/UserUploads/Product/" + image;
                FileUpload1.PostedFile.SaveAs(MapPath(filePath));
            }
            else
            {
                image = "noimage.jpg";
            }

            if (FuntionHNC.CheckFileType(image))
            {
                int i = pro.Insert(null, txtTitle.Text, txtTitle_en.Text, txtContent.Value, txtContent_en.Value, txtContent.Value, txtContent_en.Value, image, null, null, null, null, 12, DateTime.Now, DateTime.Now, "hnc", true, vip, false, FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), int.Parse(txtOrder.Text), status, false);
                if (i > 0)
                {
                    Response.Redirect("~/Admin/Project/Project.aspx");
                    lblMsg.Text = "Cập nhật thành công!";
                }
                else
                {
                    lblMsg.Text = "Cập nhật không thành công!";
                }
            }
            else
            {
                lblMsg.Text = "Định dạng ảnh phải là *.png, *.ipg";
            }
        }
        else
        {
            DataTable dt = pro.GetProductByProductID(int.Parse(Request.QueryString["ProductID"].ToString()));
            string image = "";
            if (FileUpload1.FileName.Trim() != "" && FileUpload1.FileName != null)
            {
                string[] arr = FileUpload1.FileName.Split('.');
                string ext = arr[arr.Length - 1];

                for (int j = 0; j < arr.Length - 1; j++)
                {
                    image = image + arr[j];
                }

                image = image + FuntionHNC.ThemChuoiDatetime();
                image = image + "." + arr[arr.Length - 1];

                image = image.Replace('%', '-');

                String filePath = "~/UserUploads/Product/" + image;
                FileUpload1.PostedFile.SaveAs(MapPath(filePath));
            }
            else
            {
                image = dt.Rows[0]["Image"].ToString();
            }
            if (FuntionHNC.CheckFileType(image))
            {      
                int i = pro.Update(null, txtTitle.Text, txtTitle_en.Text, txtContent.Value, txtContent_en.Value, txtContent.Value, txtContent_en.Value, image, null, null, null, null, 12, DateTime.Now, DateTime.Now, "hnc", true, vip, false, FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), FuntionHNC.RemoveUnicode(txtTitle.Text), int.Parse(txtOrder.Text), status, false, int.Parse(Request.QueryString["ProductID"].ToString()));
                if (i > 0)
                {
                    Response.Redirect("~/Admin/Project/Project.aspx");
                    lblMsg.Text = "Cập nhật thành công!";
                }
                else
                {
                    lblMsg.Text = "Cập nhật không thành công!";

                }
            }
            else
            {
                lblMsg.Text = "Định dạng ảnh phải là *.png, *.ipg";
            }
        }
    }


    public void LoadProductDetail(int proid)
    {
        DataTable dt = pro.GetProductByProductID(proid);
        //ddlCate.SelectedValue = dt.Rows[0]["CateID"].ToString();
        string image = dt.Rows[0]["Image"].ToString();
        if (image == "")
        {
            txtImage.ImageUrl = "~/UserUploads/Product/noimage.jpg";
        }
        else
        {
            txtImage.ImageUrl = "~/UserUploads/Product/" + image;
        }
        txtTitle.Text = dt.Rows[0]["Title"].ToString();
        txtTitle_en.Text = dt.Rows[0]["Title_en"].ToString();
        txtContent.Value = dt.Rows[0]["Content"].ToString();
        txtContent_en.Value = dt.Rows[0]["Content_en"].ToString();
        txtOrder.Text = dt.Rows[0]["Order"].ToString();
        string vip = dt.Rows[0]["Vip"].ToString();
        if (vip == "True")
        {
            cbVip.Checked = true;
        }
        else
        {
            cbVip.Checked = false;
        }
        string status = dt.Rows[0]["Status"].ToString();
        if (status == "True")
        {
            cbStatus.Checked = true;
        }
        else
        {
            cbStatus.Checked = false;
        }
    }
}